package com.tc.socket.controller;

import com.tc.socket.config.CustomerSubProtocolWebSocketHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketSession;

import javax.jms.Destination;
import java.io.IOException;
import java.nio.charset.Charset;
import java.security.Principal;
import java.util.Map;

/**
 * @Auther: tianchao
 * @Date: 2022/2/23 10:41
 * @Description:
 */
@RestController
@Slf4j
public class WebSocketController implements InitializingBean {
    @Autowired
    //private SimpMessagingTemplate template;
    //private SimpMessageSendingOperations template;
    private SimpMessagingTemplate template;
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @MessageMapping("/send")
    @SendTo("/topic/test")
    public String say(String msg, StompHeaderAccessor headerAccessor) {
        log.info("say user: {}", headerAccessor);
        log.info("广播消息: {},线程: {}",msg, Thread.currentThread().getName());
        return msg;
    }

    @GetMapping("/send")
    public String msgReply(@RequestParam String msg) {
        Destination destination = new ActiveMQTopic("/topic/test");
        template.convertAndSend("/topic/test", msg);
        return msg;
    }
    @MessageMapping("/sendToUser")
    public void sendToUser(Map<String,String> map,Principal principal){
        System.out.println(principal);
        String fromUserId = map.get("fromUserId");
        String toUserId = map.get("toUserId");
        String msg = "来自" +fromUserId + "的消息:"+ map.get("msg");
        //template.convertAndSendToUser(toUserId,"/topic",msg);
        //Destination destination = new ActiveMQTopic("/topic/chat/111topic");
        template.convertAndSend("/topic/chat/"+toUserId,msg);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("SimpMessagingTemplate :{}",template.getClass());
        log.info("JmsMessagingTemplate :{}",jmsMessagingTemplate.getClass());
    }

    @Autowired
    private WebSocketHandler customerSubProtocolWebSocketHandler;
    @GetMapping("close")
    public String closeBySessionId(String sessionId){
        ((CustomerSubProtocolWebSocketHandler)customerSubProtocolWebSocketHandler).close(sessionId);
        return "success";
    }

    @GetMapping("sendToSession")
    public String closeBySessionId(String sessionId,String message){
        WebSocketSession webSocketSession = ((CustomerSubProtocolWebSocketHandler) customerSubProtocolWebSocketHandler).getWebSocketSessionById(sessionId);
        try {
            webSocketSession.sendMessage(new TextMessage(message.getBytes(Charset.defaultCharset())));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "success";
    }
}
